<?php

class ActivityController extends Zend_Controller_Action {

    public function indexAction() {
        // action body
        $galleryElementModel = new Application_Model_ActivityModel();
        $taskElementModel = new Application_Model_TaskModel();
        
        //$this->view->galleryElements = $galleryElementModel->fetchAll();
        $currentPage = $this->getRequest()->getParam('page', 1);

        $auth = Zend_Auth::getInstance();
        $user = $auth->getIdentity();
        $condition = "user_id = ".$user->idusers;

        $select = $galleryElementModel->select()->where($condition);

        $adapter = new Zend_Paginator_Adapter_DbSelect($select);
        
        $paginator = new Zend_Paginator($adapter);
        $paginator->setItemCountPerPage(10);
        $paginator->setCurrentPageNumber($currentPage);
        $this->view->paginator = $paginator;
    }
    
    public function monitorAction() {
        $request = $this->getRequest();
        
        $activityId = $request->getParam("id");
        
        if ($request->isGet() && $activityId) {
            
            $this->view->activityId = $activityId;
            
        }
    }


    public function monitorserviceAction() {
        
        header("Content-Type: application/json");

        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender();
        
        $request = $this->getRequest();
        
        $activityId = $request->getParam("id");

        if ($request->isGet()) {
            
            $taskModel = new Application_Model_TaskModel();
            $tasks = $taskModel->fetchAll("idactivities = $activityId");
            $tasksArray = array();
            
            foreach ($tasks->toArray() as $index => $value) {
                $tasksArray[] = $value;

                $timeProgressCurrent = $value['progress_time'];
                $plannedTime = $value['planned_time'];
                $time = $timeProgressCurrent;
                
               if($value['status'] == 2){
                   $time = $timeProgressCurrent + 1;
                   $data = array(
                            "progress_time" => $time
                    );
                    $tableTask = new Application_Model_TaskModel();
                    $where = $tableTask->getAdapter()->quoteInto("idtasks=?",$value['idtasks'] );
                    $tableTask->update($data, $where);
                }
                $percent = ($time*100)/$plannedTime;
                $tasksArray[$index]["percentage"] = (int)$percent;
            }
            
            $arrayResponse = array(
                                    "tasks" => $tasksArray
                            );
            $jsonResponse = json_encode($arrayResponse);

            echo $jsonResponse;
        }
    }
    
    public function searchAction() {
        $query = $this->getRequest()->getParam("query");

        // action body
        $activityModel = new Application_Model_ActivityModel();

        //$this->view->galleryElements = $galleryElementModel->fetchAll();

        $currentPage = $this->getRequest()->getParam('page', 1);

        $select = $activityModel->select()
                ->where('name LIKE ?', '%' . $query . '%')
                ->orWhere('description LIKE ?', '%' . $query . '%');

        $adapter = new Zend_Paginator_Adapter_DbSelect($select);
        $paginator = new Zend_Paginator($adapter);
        $paginator->setItemCountPerPage(10);
        $paginator->setCurrentPageNumber($currentPage);
        $this->view->paginator = $paginator;
    }
    
    public function searchserviceAction() {
        header("Content-Type: application/json");

        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender();

        $request = $this->getRequest();
        
        $query = $request->getParam("query");

        if ($request->isGet()) {

            $activityModel = new Application_Model_ActivityModel();
            $select = $activityModel->select()
                        ->where('name LIKE ?', '%' . $query . '%')
                        ->orWhere('description LIKE ?', '%' . $query . '%');
            $activities = $activityModel->fetchAll($select);

            $arrayResponse = array(
                                    "activities" => $activities->toArray()
                            );
            $jsonResponse = json_encode($arrayResponse);

            echo $jsonResponse;
        }
    }
    
    public function pageserviceAction() {
        header("Content-Type: application/json");

        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender();

        $request = $this->getRequest();
        
        $page = $request->getParam("page");
        $elementsPerPage = $request->getParam("elementsPerPage");
        if (!$elementsPerPage) {
            $elementsPerPage = 10;
        }

        if ($request->isGet()) {

            $activityModel = new Application_Model_ActivityModel();
            
            
            $auth = Zend_Auth::getInstance();
            $user = $auth->getIdentity();
            $condition = "user_id = ".$user->idusers;
            $activities = $activityModel->getActivities($page, $elementsPerPage,$condition);

            $totalPages = floor($activityModel->countActivities() / $elementsPerPage);
            $totalPages += ($activityModel->countActivities() % $elementsPerPage) ? 1 : 0;

            $arrayResponse = array(
                                    "activities" => $activities->toArray(),
                                    "currentPage" => $page,
                                    "totalPages" => $totalPages
                            );
            $jsonResponse = json_encode($arrayResponse);

            echo $jsonResponse;
        }
    }

    public function saveAction() {
        $request = $this->getRequest();
        $form = new Application_Form_NewActivityForm();

        if ($request->isPost()) {
            if ($form->isValid($request->getPost())) {

                $data = $form->getValues();

                $activityModel = new Application_Model_ActivityModel();

                $activityToSave = array();
                $activityToSave["name"] = $data["name"];
                $activityToSave["description"] = $data["description"];
                $activityToSave["planned_time"] = $data["planned_time"];
                $activityToSave["start_date"] = $data["start_date"];
                $activityToSave["end_date"] = $data["end_date"];
                $activityToSave["user_id"] = Zend_Auth::getInstance()->getIdentity()->idusers;
                var_dump($activityToSave);
                $id = $activityModel->insert($activityToSave);

                return $this->_helper->redirector('index', 'Activity');
            }
        }

        $this->view->form = $form;
    }

}